#include "page1.h"

static lv_ft_info_t info1;
static lv_style_t style1;
static lv_obj_t * kb;
static lv_obj_t * ta;
static lv_obj_t * win;
static const char * btns[] = {""};
static lv_obj_t * msgbox;
static lv_obj_t * win2;

static void btn_cb1(lv_event_t * e);
static void ta_cb(lv_event_t * e);
static void yes_cb(lv_event_t * e);
static void no_cb(lv_event_t * e);
static void add_cb(lv_event_t * e);
static void sub_cb(lv_event_t * e);
static void msg_cb(lv_event_t * e);

void msg_cb(lv_event_t * e)
{
    lv_obj_add_flag(msgbox, LV_OBJ_FLAG_HIDDEN); // 隐藏对象;
}

// 购物车事件函数
void btn_cb1(lv_event_t * e)
{
    page4_show();
    lv_obj_del(page1);
}

void ok(lv_event_t * e)
{
    lv_obj_del(win2);
    writetxt();
    plist_des(purinf);
    purinf = plist_init();
}

void can(lv_event_t * e)
{
    lv_obj_del(win2);
}
// 结账事件函数
void pay(lv_event_t * e)
{
    double sum = 0;
    char temp[50];
    plist * g = purinf;
    while(g != NULL) {
        g = g->next;
        sum += g->subtotal;
        if(g == purinf) break;
    }
    snprintf(temp, sizeof(temp), "请扫码支付%.2f元", sum);
    win2 = lv_obj_create(lv_scr_act());
    lv_obj_set_size(win2, 420, 420);
    lv_obj_center(win2);

    lv_obj_t * img = lv_img_create(win2);
    lv_img_set_src(img, &ma);
    lv_obj_set_size(img, 200, 200);
    lv_obj_set_pos(img, 100, 20);

    lv_obj_t * label = lv_label_create(win2);
    lv_obj_add_style(label, &style1, 0);
    lv_label_set_text(label, temp);
    lv_obj_set_size(label, 200, 50);
    lv_obj_set_pos(label, 100, 250);

    lv_obj_t * btn1 = lv_btn_create(win2);
    lv_obj_set_size(btn1, 90, 50);
    lv_obj_set_pos(btn1, 100, 320);
    lv_obj_t * lbtn1 = lv_label_create(btn1);
    lv_obj_add_style(lbtn1, &style1, 0);
    lv_label_set_text(lbtn1, "确认");
    lv_obj_center(lbtn1);
    lv_obj_add_event_cb(btn1, ok, LV_EVENT_CLICKED, NULL);

    lv_obj_t * btn2 = lv_btn_create(win2);
    lv_obj_set_size(btn2, 90, 50);
    lv_obj_set_pos(btn2, 210, 320);
    lv_obj_t * lbtn2 = lv_label_create(btn2);
    lv_obj_add_style(lbtn2, &style1, 0);
    lv_label_set_text(lbtn2, "取消");
    lv_obj_center(lbtn2);
    lv_obj_add_event_cb(btn2, can, LV_EVENT_CLICKED, NULL);
}

// 管理员登录界面
void btn_cb3(lv_event_t * e)
{
    log_show();
    lv_obj_del(page1);
}

// 隐藏键盘
void blank_cb1(lv_event_t * e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
}

void sub_cb(lv_event_t * e)
{
    dlist * a = (dlist *)e->user_data;
    char temp[10];
    int count;
    strcpy(temp, lv_textarea_get_text(ta));
    count = atoi(temp);
    if(count > a->stock) {
        snprintf(temp, sizeof(temp), "%d", a->stock);
        lv_textarea_set_text(ta, temp);
    } else if(count > 0) {
        count--;
        snprintf(temp, sizeof(temp), "%d", count);
        lv_textarea_set_text(ta, temp);
    }
}

void add_cb(lv_event_t * e)
{
    dlist * a = (dlist *)e->user_data;
    char temp[10];
    int count;
    strcpy(temp, lv_textarea_get_text(ta));
    count = atoi(temp);
    if(count < a->stock) {
        count++;
        snprintf(temp, sizeof(temp), "%d", count);
        lv_textarea_set_text(ta, temp);
    }
}

void ta_cb(lv_event_t * e)
{
    lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN); // 取消隐藏
    lv_keyboard_set_textarea(kb, ta);
}

void btns_cb(lv_event_t * e)
{
    lv_obj_del(msgbox);
}

void yes_cb(lv_event_t * e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
    dlist * c = (dlist *)e->user_data;

    const char * ta_text = lv_textarea_get_text(ta);

    int count = atoi(ta_text ? ta_text : "0"); // 文本为空时按0处理
    if(count == 0) {
        msgbox = lv_msgbox_create(lv_scr_act(), "提示", "请输入正确的数字", btns, true);
        lv_obj_center(msgbox);
        lv_obj_t * title   = lv_msgbox_get_title(msgbox);
        lv_obj_t * content = lv_msgbox_get_text(msgbox);
        if(title) lv_obj_set_style_text_font(title, info1.font, 0);
        if(content) lv_obj_set_style_text_font(content, info1.font, 0);
    } else if(count > c->stock) {
        msgbox = lv_msgbox_create(lv_scr_act(), "提示", "库存不足", btns, true);
        lv_obj_center(msgbox);
        lv_obj_t * title   = lv_msgbox_get_title(msgbox);
        lv_obj_t * content = lv_msgbox_get_text(msgbox);
        if(title) lv_obj_set_style_text_font(title, info1.font, 0);
        if(content) lv_obj_set_style_text_font(content, info1.font, 0);
    } else {
        if(plist_change(c->name, count, purinf) == 0) {
            c->stock -= count;
            msgbox = lv_msgbox_create(lv_scr_act(), "提示", "添加购物车成功", btns, true);
            lv_obj_center(msgbox);
            lv_obj_t * title   = lv_msgbox_get_title(msgbox);
            lv_obj_t * content = lv_msgbox_get_text(msgbox);
            if(title) lv_obj_set_style_text_font(title, info1.font, 0);
            if(content) lv_obj_set_style_text_font(content, info1.font, 0);
        } else {
            c->stock -= count;
            plist_insert_tail(c->name, c->price, count, NULL, purinf);
            msgbox = lv_msgbox_create(lv_scr_act(), "提示", "添加购物车成功", btns, true);
            lv_obj_center(msgbox);
            lv_obj_t * title   = lv_msgbox_get_title(msgbox);
            lv_obj_t * content = lv_msgbox_get_text(msgbox);
            if(title) lv_obj_set_style_text_font(title, info1.font, 0);
            if(content) lv_obj_set_style_text_font(content, info1.font, 0);
        }
    }
    plist_pri(purinf);
}

void no_cb(lv_event_t * e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);  // 隐藏对象
    if(win != NULL && lv_obj_is_valid(win)) { // 检查win是否存在且有效
        lv_obj_del(win);                      // 销毁旧弹窗
        win = NULL;                           // 重置全局变量
    }
}

// 购买事件函数
void buy(lv_event_t * e)
{
    // 搜寻对应按钮
    struct doublelist * b = proinf;
    while(b->next != proinf) {
        b = b->next;
        if(e->target == b->btn) break;
    }

    if(win != NULL && lv_obj_is_valid(win)) { // 检查win是否存在且有效
        lv_obj_del(win);                      // 销毁旧弹窗
        win = NULL;                           // 重置全局变量
    }

    // 购买弹窗
    win = lv_obj_create(page1);
    lv_obj_set_size(win, 440, 280);
    lv_obj_set_pos(win, 180, 10);

    // 弹窗上再次显示图片
    lv_obj_t * img1 = lv_img_create(win);
    lv_img_set_src(img1, b->img_dsc);
    lv_obj_set_size(img1, 100, 200);
    lv_obj_set_pos(img1, 20, 20);

    char buf1[30];
    char buf2[30];
    char buf3[30];
    sprintf(buf1, "价格：%.2f", b->price);
    sprintf(buf2, "库存：%d", b->stock);
    sprintf(buf3, "名称：%s", b->name);

    // 显示名称
    lv_obj_t * name = lv_label_create(win);
    lv_obj_add_style(name, &style1, 0);
    lv_label_set_text(name, buf3);
    lv_obj_set_size(name, 220, 20);
    lv_obj_set_pos(name, 140, 10);

    // 显示价格
    lv_obj_t * price = lv_label_create(win);
    lv_obj_add_style(price, &style1, 0);
    lv_label_set_text(price, buf1);
    lv_obj_set_size(price, 220, 20);
    lv_obj_set_pos(price, 140, 40);

    // 显示库存
    lv_obj_t * stock = lv_label_create(win);
    lv_obj_add_style(stock, &style1, 0);
    lv_label_set_text(stock, buf2);
    lv_obj_set_size(stock, 220, 20);
    lv_obj_set_pos(stock, 140, 70);

    // 创建文本编辑框用于输入购买数量
    ta = lv_textarea_create(win);
    lv_textarea_add_char(ta, '0');
    lv_textarea_set_accepted_chars(ta, "0123456789");
    lv_textarea_set_max_length(ta, 6);
    lv_obj_set_size(ta, 100, 50);
    lv_obj_set_pos(ta, 200, 130);
    lv_obj_add_event_cb(ta, ta_cb, LV_EVENT_FOCUSED, NULL);

    // 减少按键
    lv_obj_t * sub = lv_btn_create(win);
    lv_obj_set_size(sub, 40, 40);
    lv_obj_set_pos(sub, 140, 130);
    lv_obj_t * lsub = lv_label_create(sub);
    lv_label_set_text(lsub, LV_SYMBOL_MINUS);
    lv_obj_center(lsub);
    lv_obj_add_event_cb(sub, sub_cb, LV_EVENT_CLICKED, b);

    // 增加按键
    lv_obj_t * add = lv_btn_create(win);
    lv_obj_set_size(add, 40, 40);
    lv_obj_set_pos(add, 320, 130);
    lv_obj_t * ladd = lv_label_create(add);
    lv_obj_center(ladd);
    lv_label_set_text(ladd, LV_SYMBOL_PLUS);
    lv_obj_add_event_cb(add, add_cb, LV_EVENT_CLICKED, b);

    // 确认按键
    lv_obj_t * yes = show_btn_label(win, 50, 40, 170, 190, &style1, "确认");
    lv_obj_add_event_cb(yes, yes_cb, LV_EVENT_CLICKED, b);

    // 取消按键
    lv_obj_t * no = show_btn_label(win, 50, 40, 270, 190, &style1, "取消");
    lv_obj_add_event_cb(no, no_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_add_event_cb(win, blank_cb1, LV_EVENT_CLICKED, NULL);
}

// 加载商品和按钮
void load_btn(lv_obj_t * parent)
{
    int i, j;
    dlist * p = proinf->next;
    for(j = 10; j < 10 + Y * 270; j += 270) {
        for(i = 40; i < 40 + X * 120; i += 120) {
            // 图片
            lv_obj_t * img = lv_img_create(parent);
            lv_img_set_src(img, p->img_dsc);
            lv_obj_set_size(img, 100, 200);

            // lv_obj_t * img = lv_img_create(parent);
            // lv_img_set_src(img, "S:/project/jpg/kele.jpg");
            // lv_img_set_src(img, p->data);
            lv_obj_set_pos(img, i, j);

            // 新建按钮
            lv_obj_t * btn = show_btn_label(parent, 50, 50, i + 25, j + 210, &style1, "购买");

            // 将按钮地址存储
            p->btn = btn;

            // 给按钮添加事件函数
            lv_obj_add_event_cb(btn, buy, LV_EVENT_CLICKED, NULL);

            p = p->next;
            if(p == proinf) break;
        }
        if(p == proinf) break;
    }
    list_pri(proinf);
}

void page1_show()
{
    // 中文字库加载
    /*FreeType uses C standard file system, so no driver letter is required.*/
    info1.name   = "/font/simkai.ttf";
    info1.weight = 16;
    info1.style  = FT_FONT_STYLE_NORMAL;
    info1.mem    = NULL;
    if(!lv_ft_font_init(&info1)) {
        LV_LOG_ERROR("create failed.");
    }
    /*Create style with the new font*/
    lv_style_init(&style1);
    lv_style_set_text_font(&style1, info1.font);
    lv_style_set_text_align(&style1, LV_TEXT_ALIGN_CENTER);

    // 新建页面1
    show_page(&page1);

    // 创建显示商品的区域
    lv_obj_t * con1 = lv_obj_create(page1);
    lv_obj_set_size(con1, 700, 440);
    lv_obj_set_pos(con1, 10, 10);

    // 加载商品和按钮
    load_btn(con1);

    // 购物车图标
    lv_obj_t * btn1 = show_btn_label(page1, 60, 60, 720, 320, &style1, "购物车");
    lv_obj_add_event_cb(btn1, btn_cb1, LV_EVENT_CLICKED, NULL);

    // 结账图标
    lv_obj_t * btn2 = show_btn_label(page1, 60, 60, 720, 400, &style1, "结账");
    lv_obj_add_event_cb(btn2, pay, LV_EVENT_CLICKED, NULL);

    // 管理员登录图标
    lv_obj_t * btn3 = show_btn_label(page1, 60, 60, 720, 20, &style1, "管理员");
    lv_obj_add_event_cb(btn3, btn_cb3, LV_EVENT_CLICKED, NULL);

    kb = lv_keyboard_create(page1);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
    lv_obj_set_size(kb, 800, 200);
    lv_obj_add_event_cb(page1, blank_cb1, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(con1, blank_cb1, LV_EVENT_CLICKED, NULL);
}